using Game.XH;
using UnityEngine;

namespace Game.Component.Character
{
	public class Face : CharacterComponent
	{
		public enum State
		{
			Default = 0,
			Blink = 1,
			Eat = 2,
			Sleep = 3,
			Happy = 4
		}

		public enum EyeState
		{
			Normal = 0,
			Close = 1,
			Happy = 2,
			Ill = 3
		}

		public enum MouthState
		{
			Normal = 0,
			Open = 1,
			Ill = 2
		}

		public enum BrowState
		{
			Normal = 0,
			Ill = 1
		}

		public int browId;

		public int eyeId;

		public int mouthId;

		public int noseId;

		public int featureId;

		public Transform eye;

		public Transform brow;

		public Transform mouth;

		public Transform nose;

		public Transform feature;

		public SpriteRenderer[] eyeSprites = new SpriteRenderer[4];

		public SpriteRenderer[] mouthSprites = new SpriteRenderer[3];

		public SpriteRenderer[] browSprites = new SpriteRenderer[2];

		private EyeState eyeState;

		private MouthState mouthState;

		private BrowState browState;

		public State state;

		private float animTime;

		private bool isClosing;

		private int eatTime;

		private bool isIll;

		public Face(Transform trans)
			: base(trans)
		{
			trans.SetLocalZ(-1E-05f);
			brow = trans.Find("Brow");
			brow.SetLocalZ(-1E-05f);
			eye = trans.Find("Eye");
			eye.SetLocalZ(-1E-05f);
			nose = trans.Find("Nose");
			nose.SetLocalZ(-1E-05f);
			mouth = trans.Find("Mouth");
			mouth.SetLocalZ(-1E-05f);
			feature = trans.Find("Feature");
		}

		public void LatedUpdate()
		{
			if (state == State.Default)
			{
				return;
			}
			if (state == State.Blink)
			{
				if (isClosing)
				{
					animTime += Time.deltaTime;
					if (animTime > 0.25f)
					{
						isClosing = false;
						ChangeAnimation(State.Default);
					}
				}
			}
			else if (state == State.Eat)
			{
				animTime += Time.deltaTime;
				if (animTime >= 0.2f)
				{
					eatTime++;
					animTime = 0f;
					if (eatTime >= 9)
					{
						mouth.SetLocalScaleXY(1f, 1f);
						ChangeAnimation(State.Default);
					}
				}
				else if (eatTime % 2 == 0)
				{
					float num = 1f - animTime / 0.1f * 0.2f;
					mouth.SetLocalScaleXY(num, num);
				}
				else
				{
					float num2 = animTime / 0.1f * 0.2f + 0.8f;
					mouth.SetLocalScaleXY(num2, num2);
				}
			}
			else if (state == State.Happy)
			{
				animTime += Time.deltaTime;
				if (animTime >= 2f)
				{
					ChangeAnimation(State.Default);
				}
			}
		}

		public void ChangeAnimation(State val)
		{
			if (state != State.Sleep || val == State.Default)
			{
				state = val;
				mouth.SetLocalScaleXY(1f, 1f);
				if (state == State.Default)
				{
					ChangeEyeState(EyeState.Normal);
				}
				else if (state == State.Blink)
				{
					isClosing = true;
					animTime = 0f;
					ChangeEyeState(EyeState.Close);
				}
				else if (state == State.Eat)
				{
					animTime = 0f;
					eatTime = 0;
					ChangeEyeState(EyeState.Happy);
					ChangeMouthState(MouthState.Normal);
				}
				else if (state == State.Sleep)
				{
					ChangeEyeState(EyeState.Close);
					ChangeMouthState(MouthState.Normal);
				}
				else if (state == State.Happy)
				{
					animTime = 0f;
					ChangeEyeState(EyeState.Happy);
					ChangeMouthState(MouthState.Normal);
				}
			}
		}

		public void SetBoxColliderEnabled(bool val)
		{
			BoxCollider2D component = transform.GetComponent<BoxCollider2D>();
			if (component != null)
			{
				component.enabled = val;
			}
			if (mouth != null)
			{
				component = mouth.GetComponent<BoxCollider2D>();
				if (component != null)
				{
					component.enabled = val;
				}
			}
		}

		public void SetBrow(int id, Transform copy)
		{
			if (browId != id)
			{
				browId = id;
				browSprites[0] = copy.GetComponent<SpriteRenderer>();
				if (browSprites[1] == null)
				{
					browSprites[1] = copy.parent.Find("Ill").GetComponent<SpriteRenderer>();
				}
				ResetBrow();
			}
		}

		public void ChangeBrowState(BrowState val)
		{
			if (browState != val)
			{
				browState = val;
				ResetBrow();
			}
		}

		private void ResetBrow()
		{
			if (isIll)
			{
				ChangeSprite(brow, browSprites[1]);
			}
			else
			{
				ChangeSprite(brow, browSprites[(int)browState]);
			}
		}

		public void SetEye(int id, Transform copy)
		{
			if (eyeId != id)
			{
				eyeId = id;
				eyeSprites[0] = copy.Find("Normal").GetComponent<SpriteRenderer>();
				if (eyeSprites[1] == null)
				{
					eyeSprites[1] = copy.parent.Find("Close").GetComponent<SpriteRenderer>();
				}
				if (eyeSprites[2] == null)
				{
					eyeSprites[2] = copy.parent.Find("Happy").GetComponent<SpriteRenderer>();
				}
				if (eyeSprites[3] == null)
				{
					eyeSprites[3] = copy.parent.Find("Ill").GetComponent<SpriteRenderer>();
				}
				ResetEye();
			}
		}

		public void ChangeEyeState(EyeState val)
		{
			if (eyeState != val)
			{
				eyeState = val;
				ResetEye();
			}
		}

		private void ResetEye()
		{
			if (isIll)
			{
				ChangeSprite(eye, eyeSprites[3]);
			}
			else
			{
				ChangeSprite(eye, eyeSprites[(int)eyeState]);
			}
		}

		public void SetNose(int id, Transform copy)
		{
			if (noseId != id && !(copy == null))
			{
				noseId = id;
				ChangeSprite(nose, copy);
			}
		}

		public void SetMouth(int id, Transform copy)
		{
			if (mouthId != id && !(copy == null))
			{
				mouthId = id;
				mouthSprites[0] = copy.Find("Normal").GetComponent<SpriteRenderer>();
				if (mouthSprites[1] == null)
				{
					mouthSprites[1] = copy.parent.Find("Open").GetComponent<SpriteRenderer>();
				}
				if (mouthSprites[2] == null)
				{
					mouthSprites[2] = copy.parent.Find("Ill").GetComponent<SpriteRenderer>();
				}
				ResetMouth();
			}
		}

		public void ChangeMouthState(MouthState val)
		{
			if (mouthState != val)
			{
				mouthState = val;
				ResetMouth();
			}
		}

		private void ResetMouth()
		{
			if (isIll && mouthState == MouthState.Normal)
			{
				ChangeSprite(mouth, mouthSprites[2]);
			}
			else
			{
				ChangeSprite(mouth, mouthSprites[(int)mouthState]);
			}
		}

		public void SetFeature(int id, Transform copy)
		{
			if (featureId != id)
			{
				featureId = id;
				ChangeSprite(feature, copy);
			}
		}

		public void IsIll(bool val)
		{
			isIll = val;
			ResetEye();
			ResetBrow();
			ResetMouth();
		}
	}
}
